import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OutsourcingWorkflowModel, OutsourcingWorkflowServiceProxy,  SupplierServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { SelectItem } from 'primeng/api';

@Component({
    selector: 'awaitQuotationForm',
    templateUrl: './AwaitQuotationForm.component.html',
    styleUrls: ['./AwaitQuotationForm.component.css']
})
export class AwaitQuotationFormComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    active = false;
    saving = false;
    tabPanels = [];
    supplierUnits = [];
    workflowid: string;
    workflow: OutsourcingWorkflowModel = new OutsourcingWorkflowModel();
    constructor(injector: Injector,
        private _supplierService: SupplierServiceProxy,
        private _outSourcingworkflowServiceProxy: OutsourcingWorkflowServiceProxy
    ) {
        super(injector);
    }
    show(id: string): void {
        this.tabPanels = []; //初始化为空
        this.workflowid = id;
        this.initForm();
        this.active = true;
        this.modal.show();
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }
    onShown(): void {
    }

    save(): void {
        console.log(this.workflow.id);
        this._outSourcingworkflowServiceProxy.outsourcingFlow_QuotationSubmit(this.workflowid).subscribe(result => {
            this.notify.info(this.l('SavedSuccessfully'));
            this.close();
            this.modalSave.emit(null);
        });
    }

    initForm(): void {
        console.log(this.workflowid);
        this._outSourcingworkflowServiceProxy.getOutsourcingWorkFlow(this.workflowid).subscribe(dto => {
            this.workflow = dto.workflowDto;
            this.supplierUnits = dto.workflowDto.supplierUnit.split(',');
            console.log(this.workflow);
            _.forEach(this.supplierUnits, viewItem => {
                let _a = { title: viewItem };
                this.tabPanels.push(_a);
            });
        });
    }
}
